package com.tangyee.home_blood_center.fragments;

import android.app.Dialog;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.hmy.popwindow.PopItemAction;
import com.hmy.popwindow.PopWindow;
import com.tangyee.home_blood_center.view.DrawableTextView;
import com.tangyee.R;
import com.tangyee.home_blood_center.utils.DialogUtils;
import com.tangyee.home_blood_center.view.DrawableTextView;

import java.util.List;

/**
 * Main Fragment，掌管顶部的一排按钮：返回ImageVIew，中间的Textview和下拉弹窗，右边的曲线/表格TextView
 *
 */
public class MainFragment extends BaseFragment {

    private Dialog mWeiboDialog;

    // 血糖表格Fragment和血糖曲线图Fragment
    private BloodSugarFragment mBloodSugarFragment;
    private BloodSugarChartFragment mBloodSugarChartFragment;

    // 血压表格Fragment和血压曲线图Fragment
    private BloodPressureFragment mBloodPressureFragment;
    private BloodPressureChartFragment mBloodPressureChartFragment;

    // 糖化血红蛋白表格Fragment和曲线图Fragment
    private HbAlcFragment mHbAlcFragment;
    private HbAlcChartFragment mHbAlcChartFragment;

    // 顶部菜单
    private TextView rightTv; // 右边的TextView
    private TextView centerTv; // 中间的TextView
    private ImageView mLeftImageView; //左边的ImageView
    private ImageView mArrowIv; // 下拉指示的ImageView

    private View customView; // 下拉弹窗的view

    private FragmentManager fm; // fragment管理者

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        fm = getChildFragmentManager();
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        final View view = inflater.inflate(R.layout.fragment_main, container, false);

        mLeftImageView = view.findViewById(R.id.btn_top_left);
        rightTv = view.findViewById(R.id.btn_top_right);
        centerTv = view.findViewById(R.id.tv_center);
        final Fragment fragment = fm.findFragmentById(R.id.fragment_content);
        if (fragment == null) { // 如果R.id.fragment_content中没有界面，添加界面
            mBloodSugarChartFragment = BloodSugarChartFragment.newInstance("todo");
            mBloodSugarFragment = BloodSugarFragment.newInstance("todo");
            mBloodPressureFragment = BloodPressureFragment.newInstance("todo");
            mBloodPressureChartFragment = BloodPressureChartFragment.newInstance("todo");
            mHbAlcFragment = HbAlcFragment.newInstance("todo");
            mHbAlcChartFragment = HbAlcChartFragment.newInstance("todo");
            fm.beginTransaction()
                    .add(R.id.fragment_content, mBloodSugarFragment, "mBloodSugarFragment")
                    .add(R.id.fragment_content, mBloodSugarChartFragment, "mBloodSugarChartFragment")
                    .add(R.id.fragment_content, mBloodPressureFragment, "mBloodPressureFragment")
                    .add(R.id.fragment_content, mBloodPressureChartFragment, "mBloodPressureChartFragment")
                    .add(R.id.fragment_content, mHbAlcFragment, "mHbAlcFragment")
                    .add(R.id.fragment_content, mHbAlcChartFragment, "mHbAlcChartFragment")
                    .hide(mHbAlcChartFragment)
                    .hide(mHbAlcFragment)
                    .hide(mBloodPressureChartFragment)
                    .hide(mBloodPressureFragment)
                    .hide(mBloodSugarChartFragment)
                    .show(mBloodSugarFragment) // 添加所有界面并设置血糖界面可见
                    .commit();
        } else { // 如果有界面，判断是哪个界面，并设置好顶部Textview
            // 可能设置不完整，一开始想可以横屏显示，后面太多坑了，禁止了横屏
            mBloodSugarFragment = (BloodSugarFragment) fm.getFragments().get(0);
            mBloodSugarChartFragment = (BloodSugarChartFragment) fm.getFragments().get(1);
            if (mBloodSugarFragment.isHidden()) {
                rightTv.setText("表格");
            }
        }
        mArrowIv = view.findViewById(R.id.down);
        centerTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showDialog((TextView) v);
            }
        });
        view.findViewById(R.id.down).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                centerTv.callOnClick();
            }
        });
        // 右边TextView点击事件，需要考虑下拉弹窗中选项是否选择
        rightTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                List<Fragment> fragments = fm.getFragments();
                FragmentTransaction trans = fm.beginTransaction();
                for (int i = 0; i < fragments.size(); i ++) {
                    trans.hide(fragments.get(i));
                }
                if ("曲线".equals(((TextView)v).getText().toString())) {
                    if ("血糖".equals(centerTv.getText())) {
                        trans.setCustomAnimations(R.anim.my_fade_in, R.anim.fade_out)
                                .show(mBloodSugarChartFragment)
                                .commit();
                    }
                    if ("血压".equals(centerTv.getText())) {
                        trans.setCustomAnimations(R.anim.my_fade_in, R.anim.fade_out)
                                .show(mBloodPressureChartFragment)
                                .commit();
                    }
                    if ("糖化血红蛋白".equals(centerTv.getText())) {
                        trans.setCustomAnimations(R.anim.my_fade_in, R.anim.fade_out)
                                .show(mHbAlcChartFragment)
                                .commit();
                    }
                    ((TextView)v).setText("表格");
                } else if ("表格".equals(((TextView)v).getText().toString())) {
                    if ("血糖".equals(centerTv.getText())) {
                        trans.setCustomAnimations(R.anim.my_fade_in, R.anim.fade_out)
                                .show(mBloodSugarFragment)
                                .commit();
                    }
                    if ("血压".equals(centerTv.getText())) {
                        trans.setCustomAnimations(R.anim.my_fade_in, R.anim.fade_out)
                                .show(mBloodPressureFragment)
                                .commit();
                    }
                    if ("糖化血红蛋白".equals(centerTv.getText())) {
                        trans.setCustomAnimations(R.anim.my_fade_in, R.anim.fade_out)
                                .show(mHbAlcFragment)
                                .commit();
                    }
                    ((TextView)v).setText("曲线");
                }
            }
        });
        // 左边返回按钮事件监听
        mLeftImageView.setOnClickListener(new View.OnClickListener(){

            @Override
            public void onClick(View v) {
                if ("表格".equals(rightTv.getText().toString())) {
                    rightTv.callOnClick();
                } else {
                    getActivity().onBackPressed();
                }
            }
        });

        return view;
    }

    /**
     * 显示弹窗
     * @param view
     */
    private void showDialog(final TextView view) {
        customView = View.inflate(getContext(), R.layout.top_dialog_layout, null);
        final DrawableTextView textView1 = customView.findViewById(R.id.tv_blood_sugar);
        final DrawableTextView textView2 = customView.findViewById(R.id.tv_blood_pressure);
        final DrawableTextView textView3 = customView.findViewById(R.id.tv_hemoglobin_A1c);
        if ("血糖".equals(view.getText().toString())) {
            textView1.setSelected(true);
            textView2.setSelected(false);
            textView3.setSelected(false);
        }
        if ("血压".equals(view.getText().toString())) {
            textView1.setSelected(false);
            textView2.setSelected(true);
            textView3.setSelected(false);
        }
        if ("糖化血红蛋白".equals(view.getText().toString())) {
            textView1.setSelected(false);
            textView2.setSelected(false);
            textView3.setSelected(true);
        }
        final PopWindow popWindow = new PopWindow.Builder(getActivity())
                .setStyle(PopWindow.PopWindowStyle.PopDown)
                .setIsShowCircleBackground(false)
                .addContentView(customView)
                .addItemAction(new PopItemAction(" \n取消\n ", PopItemAction.PopItemStyle.Cancel, new PopItemAction.OnClickListener() {
                    @Override
                    public void onClick() {
                        // 取消
                    }
                }))
                .setControlViewAnim(mArrowIv, R.anim.btn_rotate_anim_1, R.anim.btn_rotate_anim_2, true)
                .show(view);

        // 下拉弹窗第一个选项点击事件监听
        textView1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                v.setSelected(true);
                textView2.setSelected(false);
                textView3.setSelected(false);
                view.setText("血糖");
                popWindow.dismiss();
                changeRightTV();
                rightTv.callOnClick();
            }
        });
        // 下拉弹窗第二个选项点击事件监听
        textView2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                v.setSelected(true);
                textView1.setSelected(false);
                textView3.setSelected(false);
                view.setText("血压");
                popWindow.dismiss();
                changeRightTV();
                rightTv.callOnClick();
            }
        });
        // 下拉弹窗第三个选项点击事件监听
        textView3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                v.setSelected(true);
                textView2.setSelected(false);
                textView1.setSelected(false);
                view.setText("糖化血红蛋白");
                popWindow.dismiss();
                changeRightTV();
                rightTv.callOnClick();
            }
        });
    }

    // 改变一下右边的TextView
    private void changeRightTV(){
        if ("曲线".equals(rightTv.getText())){
            rightTv.setText("表格");
        } else {
            rightTv.setText("曲线");
        }
    }

    /**
     * 返回键点击事件
     * @return
     */
    @Override
    public boolean onBackPressed() {
        if ("表格".equals(rightTv.getText().toString())) {
            rightTv.callOnClick();
            return true;
        }else {
            return super.onBackPressed();
        }
    }

    /**
     * hidden Changed时更新所有fragment的UserVisibleHint
     * @param hidden
     */
    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (!hidden) {
            mBloodSugarFragment.setUserVisibleHint(mBloodSugarFragment.getUserVisibleHint());
            mBloodSugarChartFragment.setUserVisibleHint(mBloodSugarChartFragment.getUserVisibleHint());
            mBloodPressureFragment.setUserVisibleHint(mBloodPressureFragment.getUserVisibleHint());
            mBloodPressureChartFragment.setUserVisibleHint(mBloodPressureChartFragment.getUserVisibleHint());
            mHbAlcFragment.setUserVisibleHint(mHbAlcFragment.getUserVisibleHint());
            mHbAlcChartFragment.setUserVisibleHint(mHbAlcChartFragment.getUserVisibleHint());
        }
    }
}
